﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace NotebookS2.Reports.Carousel
{
    public partial class BRWS_P_ITEM : DevExpress.XtraEditors.XtraForm
    {
        public BRWS_P_ITEM()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public string ItemSelected { get; set; }
        public string ItemSelected2 { get; set; }
        public string selectedWhs { get; set; }

        public void BindProducts()
        {
            SqlConnection oConnection = new SqlConnection(this.ConnectionString);
            oConnection.Open();

            if (buttonEdit1.Text == "")
            {
                SqlCommand oCommand = new SqlCommand("SELECT distinct(ItemCode),(ROW_NUMBER() OVER(ORDER BY ItemCode asc))AS RecIndex, ItemName FROM STOCKCARD WHERE ItemStatus = @Status AND WhsCode = @Whs GROUP BY ItemCode, ItemName", oConnection);
                oCommand.Parameters.AddWithValue("@Status", "Active");
                oCommand.Parameters.AddWithValue("@Whs", selectedWhs);

                SqlDataAdapter oAdapter = new SqlDataAdapter();
                DataTable dt = new DataTable();

                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(dt);

                int iRow = dt.Rows.Count;

                if (iRow > 0)
                {
                    gridControl1.DataSource = dt;
                    gridControl1.Refresh();
                }
                else
                {
                    MessageBox.Show("No active item found!", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

                oCommand.Dispose();
            }
            else
            {
                SqlCommand oCommand = new SqlCommand("SELECT distinct(ItemCode),(ROW_NUMBER() OVER(ORDER BY ItemCode asc))AS RecIndex, ItemName FROM STOCKCARD WHERE ItemStatus = @Status AND ItemCode = @Code OR ItemName = @Name AND AND WhsCode = @Whs GROUP BY ItemCode, ItemName ", oConnection);
                oCommand.Parameters.AddWithValue("@Status", "Active");
                oCommand.Parameters.AddWithValue("@Code", buttonEdit1.Text);
                oCommand.Parameters.AddWithValue("@Name", buttonEdit1.Text);
                oCommand.Parameters.AddWithValue("@Whs",  selectedWhs);

                SqlDataAdapter oAdapter = new SqlDataAdapter();
                DataTable dt = new DataTable();

                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(dt);

                int iRow = dt.Rows.Count;

                if (iRow > 0)
                {
                    gridControl1.DataSource = dt;
                    gridControl1.Refresh();
                }
                else
                {
                    MessageBox.Show("No active item found!", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

                oCommand.Dispose();
            }

            
            oConnection.Close();
        }

        private void buttonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            BindProducts();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
           ItemSelected = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ItemCode"]).ToString();
           ItemSelected2 = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ItemName"]).ToString();
           
            this.Close();
        }

        
    }
}